package com.alipay.demo.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.demo.config.AlipayConfig;
import com.alipay.demo.config.AlipayTradeRequestEnum;
import com.alipay.demo.entity.CallbackClazz;
import com.alipay.demo.entity.NotifyClazz;
import com.alipay.demo.entity.OrderClazz;
import com.alipay.demo.service.IAliPayService;
import com.alipay.demo.util.IdGeneratorUtils;
import com.alipay.demo.util.JackJsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: zhuzhiping
 * @description:
 * @date: 2021/1/6 15:26
 */

@Service
@Slf4j
public class AliPayServiceImpl implements IAliPayService {

    @Override
    public String pay(OrderClazz orderClazz) {
        orderClazz.setOut_trade_no(IdGeneratorUtils.INSTANCE.nextId());
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGatewayUrl, AlipayConfig.appId, AlipayConfig.merchantPrivateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipayPublickey, AlipayConfig.signType);
        AlipayRequest alipayRequest = AlipayTradeRequestEnum.getRequest(orderClazz);
        //发送请求
        String result = "";
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            result = "错误的请求";
            log.error("错误的请求", e);
        }
        return result;
    }

    @Override
    public String payCallback(CallbackClazz callbackClazz) throws Exception {
        String result = "";
        Map<String, String> params = (Map<String, String>) JackJsonUtils.getMap(JackJsonUtils.getJSONStr(callbackClazz));
        log.info("payCallback参数，{}", params);
        /**
         *验证签名
         */
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipayPublickey, AlipayConfig.charset, AlipayConfig.signType);

        if (signVerified) {
            Map<String, String> rsp = new HashMap<>();
            rsp.put("out_trade_no", callbackClazz.getOut_trade_no());
            rsp.put("trade_no", callbackClazz.getTrade_no());
            result = "验签成功:" + JackJsonUtils.getJSONStr(rsp);
        } else {
            result = "验签失败:" + JackJsonUtils.getJSONStr(params);
        }
        return result;
    }

    @Override
    public String payNotify(NotifyClazz notifyClazz) throws Exception {
        String result = "";
        Map<String, String> params = (Map<String, String>) JackJsonUtils.getMap(JackJsonUtils.getJSONStr(notifyClazz));
        log.info("payNotify参数，{}", JackJsonUtils.getJSONStr(params));
        /**
         * 调用SDK验证签名
         */
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipayPublickey, AlipayConfig.charset, AlipayConfig.signType);
        if (signVerified) {
            result = "success" + JackJsonUtils.getJSONStr(params);
        } else {
            result = "fail";
        }
        return result;
    }

    @Override
    public String payQuery(OrderClazz orderClazz) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGatewayUrl, AlipayConfig.appId, AlipayConfig.merchantPrivateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipayPublickey, AlipayConfig.signType);
        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        alipayRequest.setBizContent(JackJsonUtils.getJSONStr(orderClazz));
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        return result;
    }

    @Override
    public String payRefund(OrderClazz orderClazz) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGatewayUrl, AlipayConfig.appId, AlipayConfig.merchantPrivateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipayPublickey, AlipayConfig.signType);
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        alipayRequest.setBizContent(JackJsonUtils.getJSONStr(orderClazz));
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }

    @Override
    public String payRefundQuery(OrderClazz orderClazz) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGatewayUrl, AlipayConfig.appId, AlipayConfig.merchantPrivateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipayPublickey, AlipayConfig.signType);
        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
        alipayRequest.setBizContent(JackJsonUtils.getJSONStr(orderClazz));
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }

    @Override
    public String payClose(OrderClazz orderClazz) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGatewayUrl, AlipayConfig.appId, AlipayConfig.merchantPrivateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipayPublickey, AlipayConfig.signType);
        //设置请求参数
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        alipayRequest.setBizContent(JackJsonUtils.getJSONStr(orderClazz));
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }
}
